package ar.com.e2solver.view;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.Panel;
import java.awt.Toolkit;

import javax.swing.JFrame;

import ar.com.e2solver.E2Solver;
import ar.com.e2solver.model.Puzzle;
import ar.com.e2solver.solver.SolverResult;


public class E2SolverView {
	
	private E2Solver e2Solver;
	private SolverResult result;
	private JFrame mainFrame;
	private Panel mainPanel;
	private ConfigurationView configurationView;
	
	public E2SolverView(E2Solver e2Solver) {
		this.e2Solver = e2Solver;
		
	    BorderLayout bl = new BorderLayout();
	    bl.setHgap(10);
		this.mainFrame = new JFrame("E2Solver");
	    this.mainFrame.setLayout(bl);
	    this.mainFrame.setMenuBar(getMenuBar());
		this.mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
	    clean();
	    centerFrame();
	}
	
	public void clean(){
		this.result = null;
		draw();
	}
	
	public void draw(){
		
		Panel mainPanel = new Panel();
		BorderLayout bl = new BorderLayout();
	    bl.setHgap(10);
	    mainPanel.setLayout(bl);
		
		Panel leftTopPanel = new Panel();
	    BorderLayout bl3 = new BorderLayout();
	    leftTopPanel.setLayout(bl3);
	    this.configurationView = new ConfigurationView(this.getE2Solver().getSolverConfiguration());
	    leftTopPanel.add(this.configurationView, BorderLayout.NORTH);
	    leftTopPanel.add(new ActionButtonsView(this), BorderLayout.SOUTH);
	    
	    Panel leftPanel = new Panel();
	    BorderLayout bl2 = new BorderLayout();
	    leftPanel.setLayout(bl2);
	    leftPanel.add(leftTopPanel, BorderLayout.NORTH);
	    leftPanel.add(new ResultView(this.result), BorderLayout.SOUTH);
	    
	    Puzzle puzzleToShow;
	    if(this.result != null){
	    	puzzleToShow = this.result.getBestIndividual().getPuzzle();
	    } else {
	    	puzzleToShow = new Puzzle(this.getE2Solver().getPuzzleTheme());
	    }
	    
	    mainPanel.add(new PuzzleView(puzzleToShow), BorderLayout.EAST);
	    mainPanel.add(leftPanel, BorderLayout.WEST);
	    
	    if(this.mainPanel != null){
	    	this.mainFrame.remove(this.mainPanel);
	    }
	    this.mainPanel = mainPanel;
	    this.mainFrame.add(this.mainPanel, BorderLayout.CENTER);
		this.mainFrame.pack();
		this.mainFrame.setVisible(true);
	}
	
	public void setResult(SolverResult result){
		this.result = result;
		draw();
	}
	
	private MenuBar getMenuBar() {
		MenuBar menu = new MenuBar();
		
		Menu archivo = new Menu("Archivo");
		MenuItem abrir = new MenuItem("Abrir");
		archivo.add(abrir);
		
		menu.add(archivo);
		return menu;
	}

	private void centerFrame(){
	    Toolkit toolkit = Toolkit.getDefaultToolkit();
	    Dimension screenSize = toolkit.getScreenSize();

	    int x = (screenSize.width - this.mainFrame.getWidth()) / 2;
	    int y = (screenSize.height - this.mainFrame.getHeight()) / 2;

	    this.mainFrame.setLocation(x, y);		
	}

	public E2Solver getE2Solver() {
		return e2Solver;
	}
	
	public ConfigurationView getConfigurationView() {
		return configurationView;
	}

	public void setE2Solver(E2Solver e2Solver) {
		this.e2Solver = e2Solver;
		clean();
	}

}
